package tr.gen._3bit.java._2k1s.web.action;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import tr.gen._3bit.java._2k1s.dao.model.Measurement;
import tr.gen._3bit.java._2k1s.dao.model.Node;
import tr.gen._3bit.java._2k1s.dao.model.SerialInterface;
import tr.gen._3bit.java._2k1s.dao.service.MeasurementService;

import com.opensymphony.xwork2.ActionSupport;

public class ChartAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 111841967720970268L;
	
	private MeasurementService measurementService;

	private JFreeChart chart;

	private Node node;
	
	private SerialInterface serialInterface;
	
	public String nodeChart() {
		
        TimeSeries s1 = new TimeSeries("Temperature", Minute.class);
		for (Measurement m: getMeasurementService().getLastMeasurements(getSerialInterface(), getNode(), 10)) 
			s1.addOrUpdate(new Minute(m.getDate()), m.getTemperature());
		
		TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        
		chart = ChartFactory.createTimeSeriesChart(
	            "Last 10 Measurements",  // title
	            "Time",             // x-axis label
	            "Temperature",   // y-axis label
	            dataset,            // data
	            true,               // create legend?
	            true,               // generate tooltips?
	            false               // generate URLs?
	        );
		
		return execute();
	}
	
	@Override
	public String execute() {
		return SUCCESS;
	}

	public ChartAction(MeasurementService measurementService) {
		super();
		this.measurementService = measurementService;
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}

	public SerialInterface getSerialInterface() {
		return serialInterface;
	}

	public void setSerialInterface(SerialInterface serialInterface) {
		this.serialInterface = serialInterface;
	}

	public JFreeChart getChart() {
		return chart;
	}

	private MeasurementService getMeasurementService() {
		return measurementService;
	}
	
}
